<?php
/**
 * Created by PhpStorm.
 * User: liuzhuang
 * Date: 2019/12/28
 */
namespace app\index\controller;
use think\Request;
use app\common\model\ShopModel;
use app\common\model\ShopClassModel;
use app\common\ResponseFormat;
class Shop extends BaseController
{
    //依赖注入
    public function __construct(ShopClassModel $shopClass,ShopModel $shop)
    {
        $this->shopClass = $shopClass;
        $this->shop = $shop;
    }

    //大分类列表
    public function classification(){
        $res =  $this->shopClass->get_shop_class();
        var_dump($res);
    }
    //大分类下小分类列表
    public function classInfo(Request $request){
    //      $shopCLassModel = new ShopClassModel();
        $data =  $this->shopClass->get_shop_class();
        $res = array();
        foreach ($data as $k=>$v){
            $res[$k]['pid'] = $v['shop_class_fir_name'];
            $where['shop_class_fir_id'] = $v['shop_class_fir_id'];
            $shop_detail = $this->shopClass->get_shop_detail_class($where);
           if (!$shop_detail){
               continue;
           }
            foreach($shop_detail as $key=>$value){
                $res[$k]['children'][$key] = $value['shop_class_name'];
                   }
        }
        if (!empty($res)) {

            return ResponseFormat::success_array($res);
        } else {
            return ResponseFormat::failed_array();
        }

    }

    //接收起点经纬度 $longitude, $latitude
    //附近三公里的商铺  没有分类
    public function distance(Request $request){
        $data = $request->param('name');
        //接收参数
        $shop_class_id = 1;//$data['shop_class_id'];
        $longitude = 2;//$data['longitude'];
        $latitude = 3;//$data['latitude'];
        //处理
        $array = $this->returnSquarePoint($longitude, $latitude,3000);
        $where['shop_latitude']  = array(array('gt',$array['minLat']),array('lt',$array['maxLat']));
        $where['shop_longitude']  = array(array('gt',$array['minLon']),array('lt',$array['maxLon']));
        //实例化数据库，与数据库中数据对比
        $list = $this->shop->get_shop_List($latitude,$longitude,$where);
        if (!empty($list)) {

            return ResponseFormat::success_array($list);
        } else {
            return ResponseFormat::failed_array();
        }
    }
    //该分类 附近三公里的该分类下商铺
    public function searchDistanceShop(Request $request){
        //获取店铺分类id
        $shop_class_id  = $request->get('shop_class_id');
        //获取用户的经纬度
        $longitude =  $request->get('longitude');
        $latitude =  $request->get('latitude');
        $array = $this->returnSquarePoint($longitude, $latitude,3000);
        $where['shop_latitude']  = array(array('gt',$array['minLat']),array('lt',$array['maxLat']));
        $where['shop_longitude']  = array(array('gt',$array['minLon']),array('lt',$array['maxLon']));
        $list = $this->shop->get_shop_list_detail($latitude,$longitude,$where,$shop_class_id);
        if (!empty($list)) {

            return ResponseFormat::success_array($list);
        } else {
            return ResponseFormat::failed_array();
        }
    }
    //搜索店铺
    public function searchShop(Request $request){
        //获取商店名  like
        $shop_name = $request->param('shop_name');
        $latitude = $request->param('latitude');
        $longitude = $request->param('longitude');
        $where['shop_name'] =  array('like','%'.$shop_name.'%');
        $search_detail = $this->shop->search_shop($where,$latitude,$longitude);
        if (!empty($search_detail)) {

            return ResponseFormat::success_array($search_detail);
        } else {
            return ResponseFormat::failed_array();
        }
    }

}